package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/sms")
public class SmsController {
    @Autowired
    private SmsService smsService;
    /**
     * 发送登录验证码
     * @return
     */
    @PostMapping("/sendLoginCode")
    public Result sendLoginCode(@RequestParam("phone")String phone){
        log.info("进入sendLoginCode方法了");
        // 如果手机号为空，则不让用户获取到验证码
        if(null == phone){
            return new Result(false, MessageConstant.TELEPHONE_VALIDATECODE_NOTNULL);
        }
        // 不为空就发送短信给用户
        Result result = this.smsService.sendLoginCode(phone);

        return result;
    }

    /**
     * 预约时发送验证码
     * @param phone
     * @return
     */
    @PostMapping("/sendCode")
    public Result sendCode(@RequestParam("phone")String phone){
        // 如果手机号为空，则不让用户获取到验证码
        if(null == phone){
            return new Result(false, MessageConstant.TELEPHONE_VALIDATECODE_NOTNULL);
        }
        // 不为空就发送短信给用户
        Result result = this.smsService.sendLoginCode(phone);

        return result;
    }
}
